package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Resource
    private SpecParamService specParamService;
    @Resource
    private SpuService spuService;
    /**
     * 根据spuId查询spu的所有规格参数值
     *
     * @param id spu的id
     * @return 规格参数值
     */
    @Override
    public List<SpecParamDTO> querySpecValues(Long id, Boolean searching) {
        SpuDetail spuDetail = getById(id);
        String specification = spuDetail.getSpecification();//获取规格参数json串
        Map<Long, Object> map = JsonUtils.toMap(specification, Long.class, Object.class);//将json串转换成map
        List<SpecParamDTO> specParamDTOList = specParamService.querySpecParams(spuService.getById(id).getCid3(), null, searching);//根据条件查询规格参数集合
        for (SpecParamDTO specParamDTO : specParamDTOList) {
            specParamDTO.setValue(map.get(specParamDTO.getId()));//设置value
        }
        return specParamDTOList;
    }
}
